用队列实现杨辉三角

头文件如下:

#ifndef _QUEUE_H
#define _QUEUE_H

#define Success 10000
#define Failure 10001
#define SIZE 1023

#include <stdio.h>
#include <stdlib.h>

struct queue
{
	int front;
	int rear;
	int data[SIZE];
};

typedef struct queue SQ;

int QueueInit(SQ *q);
int QueueEmpty(SQ q);
int QueueEnter(SQ *q, int e);
int QueueLength(SQ *q);
int QueueQuit(SQ *q);
int GetFront(SQ q);
int QueueClear(SQ *q);

#endif

功能函数如下:

#include "Queue.h"

int QueueInit(SQ *q)
{
	if(q == NULL)
	{
		return Failure;
	}

	q->front = q->rear = 0;
	
	return Success;
}

int QueueEmpty(SQ q)
{
	return (q.front == q.rear) ? Success : Failure;
}

int QueueEnter(SQ *q, int e)
{
	if(q == NULL)
	{
		return Failure;
	}
	
	if((q->rear + 1) % SIZE == q->front)
	{
		return Failure;
	}
	q->data[q->rear] = e;
	q->rear = (q->rear + 1) % SIZE;

	return Success;
}

int QueueQuit(SQ *q)
{
	if(q == NULL)
	{
		return Failure;
	}
	if(q->rear == q->front)
	{
		return Failure;
	}
	int e;
	e = q->data[q->front];
	q->front = (q->front + 1) % SIZE;

	return e;
}

int QueueLength(SQ *q)
{
	if(q == NULL)
	{
		return Failure;
	}

	return (q->rear - q->front + SIZE) % SIZE;
}

int GetFront(SQ q)
{
	if(q.rear == q.front)
	{
		return Failure;
	}
	return q.data[q.front];
}

int QueueClear(SQ *q)
{
	if(q == NULL)
	{
		return Failure;
	}
	q->rear = q->front;
	return Success;
}

主函数如下:

#include "Queue.h"

int main()
{
	SQ s1, s2;
	int num = 0, i = 0;

	if(QueueInit(&s1) == Failure || QueueInit(&s2) == Failure)
	{
		printf("Init Failure!\n");
		return -1;
	}

	QueueEnter(&s1, 0);
	QueueEnter(&s1, 1);
	QueueEnter(&s1, 0);

	printf("Please input a number:\n");
	scanf("%d", &num);

	for(i = 0; i < num; i++)
	{
		s2 = s1;
		QueueQuit(&s1);
		while(GetFront(s1) != 0)
		{
			printf("%d	", QueueQuit(&s1));
		}
		QueueQuit(&s1);
		putchar('\n');

		QueueEnter(&s1, 0);
		do
		{
			QueueEnter(&s1, QueueQuit(&s2) + GetFront(s2));
		}while(GetFront(s2) != 0);
		QueueEnter(&s1, 0);
	}

	return 0;
}

 

 

 

在C语言中,我们可以使用数组来模拟队列,并通过动态内存分配以及循环结构来实现杨辉三角杨辉三角是一个数学术语,它表示的是一个由数字组成的三角形,每一行都是前一行的数字之和,第一行和第二行只包含一个数字。 以下是一个简单的示例,展示了如何用C语言实现杨辉三角队列生成: ```c #include <stdio.h> #include <stdlib.h> typedef struct Node { int data; struct Node *next; } QueueNode; // 队列结构体及基本操作 QueueNode* create_queue() { return (QueueNode*)malloc(sizeof(QueueNode)); } void enqueue(QueueNode** head, int value) { QueueNode* newNode = create_queue(); newNode->data = value; newNode->next = *head; *head = newNode; } int dequeue(QueueNode** head) { if (*head == NULL) { printf("Queue is empty.\n"); return -1; } int value = (*head)->data; QueueNode* temp = *head; *head = (*head)->next; free(temp); return value; } void print_triangle(int n) { for (int i = 0; i <= n; i++) { for (int j = 0; j <= i; j++) { printf("%d ", enqueue(&queue_head, i > 0 ? triangle[j] : 1)); } printf("\n"); } } int main() { QueueNode* queue_head = create_queue(); // 初始化队列头部 int n = 5; // 指定杨辉三角的大小 int triangle[n][n]; // 存储三角形数值 // 填充杨辉三角的值 for (int i = 0; i < n; i++) { triangle[i][0] = 1; triangle[i][i] = 1; } // 计算并打印三角形 for (int i = 1; i < n; i++) { for (int j = 1; j < i; j++) { triangle[i][j] = triangle[i - 1][j - 1] + triangle[i - 1][j]; } print_triangle(i); } return 0; } ``` 在这个例子中,我们使用了队列来存储当前行的数字,每次计算完一行后,就将结果加入队列,然后从队列里读取元素来打印杨辉三角。注意,这个程序假设了队列内部实现了先进先出(FIFO)的原则,所以数据会按照添加的顺序输出。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值